directives.directive('echartsBox', ['$templateCache','tools',function ($templateCache,tools) {
	return {
		restrict: 'E',
		scope: {
			myOption: '='
		},
		replace: true,
		templateUrl: 'public/views/tpl/common/echarts.html',
		link: function (scope, iElement, iAttrs) {
			var charts = echarts.init($(iElement).find('.chartBox')[0]);

			console.log(scope.myOption);
			
			charts.setOption(scope.myOption.option,true);
			
			// charts.resize({
			// 	'width': $(iElement).parent().width(),
			// 	'height': $(iElement).parent().height()
			// });

			// tools.$post(scope.myOptions.post).then(function(){

			// },function(resp){

			// },function(errResp){});
		}
	};
}]);
directives.directive('myImgUpload', ['tools','Session',function (tools,Session) {
	return {
		restrict: 'A',
		scope:{
			fileupOk: '&'
		},
		link: function (scope, iElement, iAttrs) {

			iElement.bind('change',function(){
				scope.fileupOk();				
			});
		}
	};
}])
directives.directive('ngCompare', ['$rootScope',function ($rootScope) {

 	function inherit(child, parent) {
 		child.prototype = new parent();
 		child.prototype.constructor = child;
 	}

 	function CompareUtil(){}

 	CompareUtil.prototype.compareResult = function(){
 		var compare = this.getCompare(this.compare);
 		var compareTo = this.getCompareTo(this.compareTo);
		// console.log('compare:'+compare+" compareTo:"+compareTo);
		// console.log(this.op);
		switch (this.op) {
			case 'eqeq':
				// 等于
				return compare == compareTo;
				break;
			case 'eqeqeq':
				// 全等于
				return compare === compareTo;
				break;
			case 'lt':
				// 小于
				return compare < compareTo;
				break;
			case 'gt':
				// 大于
				return compare > compareTo;
				break;
			case 'lteq':
				// 小于等于
				return compare <= compareTo;
				break;
			case 'gteq':
				// 大于等于
				return compare >= compareTo;
				break;
			case 'RegExp':
				// 正则匹配

				// console.log(compareTo); 
				if(compareTo){
					return compareTo.test(compare);
				}
				
				break;
			default:
				// statements_def
				console.log('op value is error:'+this.op);
				break;
			}
		}
	CompareUtil.prototype.getCompare = function(value){
		//number
		if(!isNaN(value)){
			return value*1;
		}
		//date
		if(/^(\d{4})\/(\d{1,2})\/(\d{1,2})$/.test(value)){
			return new Date(value).getTime();
		}
		//string
		return value;
	}
	CompareUtil.prototype.getCompareTo = function(value){
		// console.log('getCompareTo:')
		// console.log(value);
		if(this.op == "RegExp"){
			switch (value) {
				case 'mobile':
					// 手机号
					return /^1\d{10}$/;
					break;
				case 'telephone':
					// 电话
					return /^0\d{2,3}-?\d{7,8}$/;
					break;
				case 'email':
					// 邮箱
					return /^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/;
					break;
				case 'number':
					// 数字 0-9
					return /^[0-9]*$/;
					break;
				case 'carded':
					// 身份证号
					return /^\d{15}|\d{18}$/;
					break;
				default:
					// statements_def
					console.log('not define RegExp:'+value);
					break;
			}
		}else{
			return this.getCompare(value);
		}
	}



	/**
	 * @op  数值比较方式 eqeq,lt,gt
	 * @compare  值
	 * @compareTo 要比较的值
	 */
	 function Compare(op,compare,compareTo){
	 	this.op = op;
	 	this.compare = compare;
	 	this.compareTo = compareTo;
	 }

	 inherit(Compare,CompareUtil);


	 return {
	 	restrict: 'A',
	 	require: 'ngModel',
	 	link: function (scope, element, attrs,ctrl) {

	 		var op = attrs.compareOp;			
	 		ctrl.$validators.compareTo = function(val) {
	 			// console.log(attrs.compareTo);
	 			
	 			if(op=="RegExp"){
	 				var compareTo = attrs.compareTo;
	 				// console.log('RegExp');
	 			}else{
	 				var compareTo = scope.$eval(attrs.compareTo);
	 			}

	 			var compare = new Compare(op,val,compareTo);
	 			console.log('val:'+val+" "+op+" compareTo:"+compareTo+" is "+compare.compareResult())
	 			return compare.compareResult();
                // return val == scope.$eval(attrs.compareTo);
            };

            scope.$watch(attrs.compareTo, function() {
            	ctrl.$validate();
            });
        }
    };
}]);
directives.directive('scrollBars', function () {
    return {
        restrict: 'A',
        scope: { theme: "@", position: "@",autoHide:"@",axis:"@" },
        link: function (scope, element, attrs) {
            if (!scope.autoHide) {
                scope.autoHide = true;
            }
            if(!scope.axis){
                scope.axis = 'y';
            }
            $(element).mCustomScrollbar({
                axis: scope.axis,
                theme: scope.theme,
                scrollInertia: 200,
                autoDraggerLength: true,
                autoExpandScrollbar: false,
                autoHideScrollbar: scope.autoHide,
                scrollbarPosition: scope.position,
                contentTouchScroll: true,
                keyboard: { enable: true },
                mouseWheel: { preventDefault: false },
                advanced: { autoScrollOnFocus: false }
            });
        }
    };
});
directives.directive('chartsBar', ['$rootScope',function ($rootScope) {
	return {
		restrict: 'E',
		scope:{
			myOption: '='
		},
		replace: true,
		templateUrl: 'public/views/tpl/industry/charts/chartsPie.html',
		link: function (scope, iElement, iAttrs) {
			var charts = echarts.init($(iElement).find('.chartBox')[0]);

			console.log(scope.myOption);
			
			charts.setOption(scope.myOption.option,true);
			
			// charts.resize({
			// 	'width': $(iElement).parent().width(),
			// 	'height': $(iElement).parent().height()
			// });
		}
	};
}])
directives.directive('chartsPie', ['$templateCache','tools',function ($templateCache,tools) {
	return {
		restrict: 'E',
		scope: {
			myOption: '='
		},
		replace: true,
		templateUrl: 'public/views/tpl/industry/charts/chartsPie.html',
		link: function (scope, iElement, iAttrs) {
			var charts = echarts.init($(iElement).find('.chartBox')[0]);

			console.log(scope.myOption);
			
			charts.setOption(scope.myOption.option,true);
			
			// charts.resize({
			// 	'width': $(iElement).parent().width(),
			// 	'height': $(iElement).parent().height()
			// });

			// tools.$post(scope.myOptions.post).then(function(){

			// },function(resp){

			// },function(errResp){});
		}
	};
}]);